package com.quanyan.user.service.impl;

import com.quanyan.common.http.HttpClientUtils;
import com.quanyan.common.utils.GsonUtils;
import com.quanyan.user.mapper.TbUserFansMapper;
import com.quanyan.user.mapper.TbUserFollowMapper;
import com.quanyan.user.mapper.TbUserFriendMapper;
import com.quanyan.user.model.db.*;
import com.quanyan.user.model.vo.response.UserRelation;
import com.quanyan.user.service.UserRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhang on 16/11/9.
 */
@Service
public class UserRelationServiceImpl implements UserRelationService {

    //1：关注,2：粉丝,3：好友,4：无关系（陌生人）
    private static final int RELATION_FOLLOW = 1;
    private static final int RELATION_FANS = 2;
    private static final int RELATION_FRIEND = 3;
    private static final int RELATION_STRANGER = 4;

    @Value("${user.relation.url}")
    private String relationUrl;

    @Autowired
    private TbUserFriendMapper userFriendMapper;

    @Autowired
    private TbUserFollowMapper userFollowMapper;

    @Autowired
    private TbUserFansMapper userFansMapper;

    /**
     * 批量用户关注
     * @param uid   关注人
     * @param followIds  被关注人
     */
    @Override
    @Async
    public void UserFollow(Integer uid, List<Integer> followIds) {
        Map<String,Object> map = new HashMap<>();
        map.put("ver","1.0");
        map.put("protocol_ver",1);
        map.put("currentUid",uid);
        map.put("addList",followIds);
        HttpClientUtils httpClientUtils = new HttpClientUtils();
        String json = GsonUtils.toJson(map);
        try {
            httpClientUtils.postJson(relationUrl, json);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取目标用户关系
     * 注意:返回的UserRelation中,只有备注和关系类型两个字段有效
     * uid==targetUid这种情况在上游判断过
     * 如果存在多种关系,则返回的优先级:好友>关注>粉丝
     * @param uid 本人UID
     * @param targetUid 目标用户uid
     * @return
     */
    @Override
    public UserRelation getUserRelationType(int uid, int targetUid) {
        UserRelation userRelation = new UserRelation();
        userRelation.setRemark("");
        userRelation.setRelationType(RELATION_STRANGER);

        if(uid < 0 || targetUid < 0 || uid == targetUid){
            return userRelation;
        }

        TbUserFriendExample exampleFriend = new TbUserFriendExample();
        TbUserFriendExample.Criteria criteriaFriend = exampleFriend.createCriteria();

        criteriaFriend.andUidEqualTo(uid);
        criteriaFriend.andFriendIdEqualTo(targetUid);
        criteriaFriend.andStateEqualTo((byte)0);
        List<TbUserFriend> tbUserFriendList = userFriendMapper.selectByExample(exampleFriend);
        if(!tbUserFriendList.isEmpty()){
            userRelation.setRelationType(RELATION_FRIEND);
            userRelation.setRemark(tbUserFriendList.get(0).getComment());
            return userRelation;
        }

        TbUserFollowExample exampleFollow = new TbUserFollowExample();
        TbUserFollowExample.Criteria criteriaFollow = exampleFollow.createCriteria();
        criteriaFollow.andUidEqualTo(uid);
        criteriaFollow.andFollowIdEqualTo(targetUid);
        criteriaFollow.andStateEqualTo((byte)0);

        List<TbUserFollow> tbUserFollowList = userFollowMapper.selectByExample(exampleFollow);
        if(!tbUserFollowList.isEmpty()){
            userRelation.setRelationType(RELATION_FOLLOW);
            userRelation.setRemark(tbUserFollowList.get(0).getComment());
            return userRelation;
        }

        TbUserFansExample exampleFans = new TbUserFansExample();
        TbUserFansExample.Criteria criteriaFans = exampleFans.createCriteria();

        criteriaFans.andUidEqualTo(uid);
        criteriaFans.andFansIdEqualTo(targetUid);
        criteriaFans.andStateEqualTo((byte)0);

        List<TbUserFans> tbUserFansList = userFansMapper.selectByExample(exampleFans);
        if(!tbUserFansList.isEmpty()){
            userRelation.setRelationType(RELATION_FANS);
            return userRelation;
        }




        return userRelation;
    }
}
